#include "main.h"
#include <math.h>

/*判断非负整数的位数*/
static unsigned int int_digit(unsigned int a)
{
	int i = 0;
	while(1)
	{
		a = a/10;
		i++;
		if(a == 0)	break;
	}

	return i;
}

/*validdata转换为msg*/
void vdata_to_msg(void)
{
	msg.zgno = validdata[0];
	msg.tem[0] = validdata[1];
	msg.tem[1] = validdata[2];
	msg.hum[0] = validdata[3];
	msg.hum[1] = validdata[4];
	msg.lux[0] = validdata[5];
	msg.lux[1] = validdata[6];
}

/*msg转换为info*/
void msg_to_info(void)
{
	info.zgno = msg.zgno;
	info.temper = msg.tem[1]+(float)msg.tem[0]/pow(10,int_digit(msg.tem[0]));
	info.humi = msg.hum[1]+(float)msg.hum[0]/pow(10,int_digit(msg.hum[0]));
	info.illum = msg.lux[1]<<8 | msg.lux[0];
	info.dev_stat = 0;//目前暂时无法读取设备状态，按照关闭处理
}

/*info转换为mobilebuf*/
void info_to_mobilebuf(void)
{
	bzero(mobilebuf,size);
	pthread_mutex_lock(&mutex);//上锁
	sprintf(mobilebuf, "%u#%0.1f#%0.1f#%u",info.dev_stat, info.temper, info.humi, info.illum);
	pthread_mutex_unlock(&mutex);//释放锁
}


